package com.ruoyi.web.controller.system;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.framework.util.ShiroUtils;
import com.ruoyi.system.domain.DataModuleType;
import com.ruoyi.system.domain.SysUser;
import com.ruoyi.system.service.ISysModelManageService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

/**
 * 模型类别控制层
 */
@Controller
@RequestMapping("/system/modelType")
public class SysModelManageController extends BaseController {
    @Autowired
    private ISysModelManageService modelManageService;

    private String prefix = "system/modelType";

    @RequiresPermissions("system:modelType:view")
    @GetMapping()
    public String modelType()
    {
        return prefix + "/modelType";
    }

    /**
     * 根据条件查询模型类别
     * @param moduleType
     * @return
     */
    @RequiresPermissions("system:modelType:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(DataModuleType moduleType)
    {
        startPage();
        List<DataModuleType> moduleTypes = modelManageService.selectAllModelType(moduleType);
        return getDataTable(moduleTypes);
    }

    /**
     * 根据ids删除模型类别记录
     * @param ids
     * @return
     */
    @RequiresPermissions("system:modelType:remove")
    @Log(title = "模型类别管理", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids){
        return toAjax(modelManageService.deleteModelTypeByIds(Convert.toStrArray(ids)));
    }

    /**
     * 新增模型类型页面
     */
    @RequiresPermissions("system:modelType:add")
    @Log(title = "模型类别管理", businessType = BusinessType.INSERT)
    @GetMapping("/add")
    public String add(ModelMap mp)
    {
        // 取身份信息
        SysUser user = ShiroUtils.getSysUser();
        mp.put("user",user);
        return prefix + "/add";
    }

    /**
     * 新增模型类型
     * @param moduleType
     * @return
     */
    @RequiresPermissions("system:modelType:add")
    @Log(title = "模型类别管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(@Validated DataModuleType moduleType,
                              @RequestParam("file") MultipartFile file) throws IOException
    {
        if(file == null || file.isEmpty()){
            return toAjax(0);
        }
        //文件上传
        String fileName = FileUploadUtils.upload(file);
        moduleType.setPicUrl(fileName.substring(1));//因为默认地址前面会多一个斜杠，导致地址解析错误，去掉就好了
        moduleType.setCreateId(ShiroUtils.getSysUser().getUserId().toString());
        moduleType.setCreateName(ShiroUtils.getSysUser().getLoginName());
        return toAjax(modelManageService.insertModelTypeInfo(moduleType));
    }

    /**
     * 检查模型类别名称是否重复
     */
    @PostMapping("/checkModelTypeNameUnique")
    @ResponseBody
    public Boolean checkModelTypeNameUnique(@Validated DataModuleType moduleType)
    {
        return modelManageService.checkModelTypeNameUnique(moduleType);
    }




    /**
     * 修改品牌信息
     */
    @RequiresPermissions("system:modelType:edit")
    @Log(title = "模型类型管理", businessType = BusinessType.UPDATE)
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") String id, ModelMap mmap)
    {
        // 取身份信息
        SysUser user = ShiroUtils.getSysUser();
        mmap.put("user",user);
        mmap.put("modelType", modelManageService.selectModelTypeById(Integer.valueOf(id)));
        return prefix + "/edit";
    }

    /**
     * 供应商信息更新
     */
    @RequiresPermissions("system:modelType:edit")
    @Log(title = "模型类型管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult edit(@Validated DataModuleType moduleType,
                           @RequestParam("file") MultipartFile file) throws IOException
    {
        if(file!=null && !file.isEmpty()){//判断MultipartFile文件判断是否存在
            //文件上传
            String fileName = FileUploadUtils.upload(file);
            moduleType.setPicUrl(fileName.substring(1));
        }
        //添加用户信息
        moduleType.setUpdateId(ShiroUtils.getSysUser().getUserId().toString());
        moduleType.setUpdateName(ShiroUtils.getSysUser().getLoginName());
        return toAjax(modelManageService.updateModelTypeById(moduleType));
    }






}
